layui.use(['layer', 'form','table'], function() {
    const layer = layui.layer;
    const $ = layui.$;
    const table = layui.table;
    const form = layui.form;

    let modelTab = table.render({
        elem: '#modelTab',
        url: '/activiti/model/list', //数据接口
        page: true, //开启分页
        request: {
            pageName: 'pageNum', // page
            limitName: 'pageSize' // limit
        },
        cols: [[ //表头
            {field: 'id', title: '模型ID', width:120, sort: true, fixed: 'left'},
            {field: 'name', title: '模型名称',minWidth:200},
            {field: 'revision', title: '修订号',width:100},
            {field: 'key', title: '键值',width:120},
            {field: 'deploymentId', title: '发布号',width:120},
            {field: 'version', title: '版本号',width:100},
            {field: 'createTime', title: '创建时间',templet:'<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'},
            {field: 'lastUpdateTime', title: '最近更新时间',templet:'<div>{{ layui.util.toDateString(d.lastUpdateTime, "yyyy-MM-dd HH:mm:ss") }}</div>'},
            {field: 'operate', title: '操作',toolbar: '#operate',width:200}
        ]],
        parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
            return {
                "code": res.code, //返回状态码
                "msg": res.msg, //解析提示信息
                "count": res.data.total, //后台返回的数据总条数，用于自定义分页使用
                "data": res.data.list //解析数据列表
            };
        }
    });

    form.on('submit(query)',function(data){
        modelTab.reload({
            where: data.field
            ,page: {
                curr: 1
            }
        });
        return false
    });


    form.on('submit(add)',function(){
        let html = document.getElementById('addModel').innerHTML;
        layer.open({
            title: '新增模型',
            type: 1,
            content: html,
            area: ['650px', '400px'],
            btn: ['确定', '取消'],
            yes: function(){
                $('#save').click();
            },
            btn2: function(){
                layer.closeAll();
            }
        });
        return false;
    });


    form.on('submit(save)',function(data){
        $.ajax({
            url:"/activiti/model/create",
            dataType:"json",
            data:data.field,
            type:"post",
            success:function(res){
                if (res.code==0){
                    layer.closeAll();
                    window.location.href = '/activiti/modeler.html?modelId=' + res.data.id ;
                }else{
                    layer.msg(res.msg,{icon:5})
                }
            },
            error:function(){
                layer.msg("系统错误",{icon:5})
            }
        })
    }) ;



    table.on('tool(modelTab)',function(obj){
       if (obj.event == 'edit'){
            window.location.href = "/activiti/model/edit?modelId="+obj.data.id;
       } else if (obj.event == 'del'){
           layer.confirm('确定要删除模型?',{
               title: false,
               btn: ['确定','取消'] //按钮
           }, function(ind){
               layer.close(ind);
               $.ajax({
                   url:"/activiti/model/delete/"+obj.data.id,
                   type:"GET",
                   success:function(res){
                       if (res.code==0){
                           modelTab.reload()
                           layer.msg(res.msg,{icon:6})
                       }else{
                           layer.msg(res.msg,{icon:5})
                       }
                   },
                   error:function(){
                       layer.msg("系统错误",{icon:5},3000)
                   }
               })
           }, function(inds){
               layer.close(inds);
           })
       }else if (obj.event == 'deploye'){
           $.ajax({
               url:"/activiti/model/deploye",
               dataType:"json",
               data:{
                   modelId:obj.data.id
               },
               type:"post",
               success:function(res){
                   if (res.code==0){
                       layer.msg(res.msg,{icon:6})
                       modelTab.reload()
                   }else{
                       layer.msg(res.msg,{icon:5})
                   }
               },
               error:function(){
                   layer.msg("系统错误",{icon:5})
               }
           })
       }
    })

})